from django.shortcuts import render_to_response, get_object_or_404
from ximport.models import UploadFile
from tradinglist.models import TradinglistNorm

def monthly_stat(request, pk):
    f = get_object_or_404(UploadFile, pk = pk)
    trades = TradinglistNorm.objects.filter(csv_file_id__pk = pk).order_by("entry_date")

#    print f

    ar_complete_stat = {}
    
    for t in trades:
        m = "%d-%02d" % (t.entry_date.year, t.entry_date.month)
        if ar_complete_stat.has_key(m) == False:
            ar_complete_stat[m] = {"profit": 0.0, "trades": []}

#        if ar_stat.has_key(m) == False:
#            ar_stat[m] = 0.0
#        ar_stat[m] = ar_stat[m] + t.get_profit()

        ar_complete_stat[m]["profit"] += t.get_profit()
        ar_complete_stat[m]["trades"].append(t)
        
        #ar_stat_2011[m] = ar_stat_2011[m] + t.get_profit()
        
     
    return render_to_response("tradinglist/monthlystat.html", {"f": f, "ar_complete_stat": ar_complete_stat})

def monthly_info(request, pk):
    f = get_object_or_404(UploadFile, pk = pk)
    trades = TradinglistNorm.objects.filter(csv_file_id__pk = pk).order_by("entry_date")

#    print f

    ar_complete_stat = {}
    
    for t in trades:
        m = "%d-%02d" % (t.entry_date.year, t.entry_date.month)
        if ar_complete_stat.has_key(m) == False:
            ar_complete_stat[m] = {"profit": 0.0, "trades": []}

#        if ar_stat.has_key(m) == False:
#            ar_stat[m] = 0.0
#        ar_stat[m] = ar_stat[m] + t.get_profit()

        ar_complete_stat[m]["profit"] += t.get_profit()
        ar_complete_stat[m]["trades"].append(t)
        
        #ar_stat_2011[m] = ar_stat_2011[m] + t.get_profit()
     
    return render_to_response("tradinglist/monthlyinfo.html", {"f": f, "ar_complete_stat": ar_complete_stat})
